//
// Created by Administrator on 2023/11/24.
//
#include "bits/stdc++.h"

using namespace std;

const int N = 1e3+10;
int dp[N][N], v[N], p[N];

int main() {
    int c, n;
    cin >> c >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> p[i] >> v[i];
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= c; ++j) {
            if (p[i] > j)
                dp[i][j] = dp[i - 1][j];
            else {
                dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - p[i]] + v[i]);
            }
        }
    }
    cout << dp[n][c];
}